using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace Wsp365.AutomaticContentDatabaseSelector.Features.SelectSmallestContentDatabase
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("862450b6-b988-424c-9eee-507c86b3fcca")]
    public class SelectSmallestContentDatabaseEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebService contentService = SPWebService.ContentService;

                //validate if there is another third party product using the site creation provider
                if (contentService.SiteCreationProvider != null
                    && !(contentService.SiteCreationProvider is SmallestContentDatabaseSiteCreationProvider))
                {
                    throw new ArgumentException("SPWebService.ContentService.SiteCreationProvider is already set!");
                }
                else
                {
                    contentService.SiteCreationProvider = new SmallestContentDatabaseSiteCreationProvider();
                    contentService.Update();
                }
            }
            catch (Exception e)
            {
                //we don't want to complete the activation
                throw e;
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                RemoveSiteCreationProvider();
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(4,
                    new SPDiagnosticsCategory(Constants.DiagnosticCategoryName, TraceSeverity.High, EventSeverity.ErrorCritical),
                    TraceSeverity.High,
                    "Failed to remove the site creation provider. Exception: {0} \n StackTrace:{1}",
                    ex.Message, ex.StackTrace);
            }
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            try
            {
                RemoveSiteCreationProvider();
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(4,
                    new SPDiagnosticsCategory(Constants.DiagnosticCategoryName, TraceSeverity.High, EventSeverity.ErrorCritical),
                    TraceSeverity.High,
                    "Failed to remove the site creation provider. Exception: {0} \n StackTrace:{1}",
                    ex.Message, ex.StackTrace);
            }
        }

        private void RemoveSiteCreationProvider()
        {
            SPWebService contentService = SPWebService.ContentService;

            if (contentService.SiteCreationProvider != null
                && contentService.SiteCreationProvider is SmallestContentDatabaseSiteCreationProvider)
            {
                contentService.SiteCreationProvider = null;
                contentService.Update();
            }
        }
    }
}
